package com.rqian.spring.profile.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rqian.spring.common.util.MD5Util;
import com.rqian.spring.profile.bean.LoginUser;
import com.rqian.spring.profile.bean.UserProfileBean;
import com.rqian.spring.profile.exception.CheckLoginUserDataException;
import com.rqian.spring.profile.exception.MD5Exception;
import com.rqian.spring.profile.service.LoadUserDataService;
import com.rqian.spring.profile.service.LoginService;

/**
 * 
 * Hash password with MD5, then validate the login info is correct.
 * 
 * @author: rqian
 * @version: 1.0, Apr 26, 2012
 */
@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoadUserDataService	loadUserDataService;



	@Override
	public boolean checkLoginUserData(LoginUser user) {
		String passwdMD5 = "";
		try {
			passwdMD5 = MD5Util.hashStrWithMD5(user.getPassword());
		} catch (MD5Exception e) {
			throw new CheckLoginUserDataException(passwdMD5 + " | " + e.getErrorMessage());
		}

		if (passwdMD5 == null) {
			throw new CheckLoginUserDataException();
		}

		user.setPasswordWithMD5(passwdMD5);

		UserProfileBean profile = loadUserDataService.loadUserProfileWithLoginUserData(user);
		if (profile == null) {
			return false;
		}

		return true;
	}

}
